from flask import current_app
from flask import g, jsonify
from flask import redirect
from flask import render_template
from flask import request
from hjutils.image_storage import image_storage

from info import constants
from info import db
from info.models import News, Category
from info.utils.commons import user_login_data
from info.utils.response_code import RET
from . import profile_blue


#获取我的关注
@profile_blue.route('/user_follow')
@user_login_data
def user_follow():
    #1获取参数
    page = request.args.get("p","1")
    #2参数类型转换
    try:
        page = int(page)

    except Exception as e:
        page = 1

    #3分页查询收藏的新闻
    try:
        paginate = g.user.followed.paginate(page,2,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取作者失败")
    #4获取分页对属性,总页数.当前页 ,当前页对象列表
    totalPage = paginate.pages
    currentPage = paginate.page
    items = paginate.items
    #5将对象列表.转成字典列表
    author_list = []
    for author in items:
        author_list.append(author.to_dict)
    #6拼接数据,渲染页面
    data = {
        "totalPage":totalPage,
        "currentPage":currentPage,
        "author_list":author_list
    }
    return render_template("news/user_follow.html",data=data)
# 用户新闻列表获取
@profile_blue.route('/news_list')
@user_login_data
def news_list():
    #1参数获取p
    page = request.args.get('p',1)
    #2参数类型转换
    try:
        page = int(page)
    except Exception as e:
        page = 1

    #3分页查询用户发布的新闻
    try:
        paginate = News.query.filter(News.user_id == g.user.id).order_by(News.create_time.desc()).paginate(page,10,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    #4获取分页对象属性,总页数.当前页.当前页对象列表
    totalpage = paginate.pages
    currentPage = paginate.page
    items = paginate.items
    #5将对象列表/转成字典
    news_list = []
    for news in items:
        news_list.append(news.to_review_dict())
    #6拼接数据.渲染页面
    data = {
        "totalPage":totalpage,
        "currentPage":currentPage,
        "news_list":news_list
    }
    return render_template("news/user_news_list.html",data=data)




# 新闻发布接口
@profile_blue.route('/news_release',methods=['GET','POST'])
def news_release():
    #1判断请求方式.如果是get
    if request.method == "GET":
    #2携带分类数据渲染页面
        try:
            categories = Category.query.all()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR,errmsg="获取分类失败")
        category_list = []
        for category in categories:
            category_list.append((category.to_dict()))

        return render_template("news/user_news_release.html",categories=category_list)
    #3如果是post,获取参数
    title = request.form.get("title")
    category_id = request.form.get("category_id")
    digest = request.form.get("digest")
    index_image = request.form.get("index_image")
    content = request.form.get("content")
    #效验参数,为空效验
    if not all([title,category_id,digest,index_image,content]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")
    #上传图片,判断是否上传成功
    try:
        image_name = image_storage(index_image.read())
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg="七牛云异常")
    if not image_name:
        return jsonify(errno=RET.NODATA,errmsg="图片上传失败")
    #创建新闻对象设置属性
    news = News()
    news.title = title
    news.source = g.user.nick_name
    news.digest = digest
    news.content = content
    news.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name
    news.category_id = category_id
    news.user_id = g.user.id
    news.status = 1
    #保存到数据
    try:
        db.session.add(news)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="新闻发布失败")

    #返回响应
    return jsonify(errno=RET.OK,errmsg="发布成功")


# 查询获取收藏分页的新闻接口
@profile_blue.route('/collection')
@user_login_data
def collection():
    #1获取参数
    page = request.args.get("p","1")
    #2参数类型转换
    try:
        page = int(page)
    except Exception as e:
        page = 1
    #3分页查询收藏的新闻
    try:
        paginate = g.user.collection_news.order_by(News.create_time.desc()).paginate(page,2,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    #4获取分页对象属性,总页数,当前页.当前页对象列表
    totalPage = paginate.pages
    currentPage = paginate.page
    items = paginate.items
    #5将对象列表转成字典列表
    news_list = []
    for news in items:
        news_list.append(news.to_dict())
    #6拼接数据.渲染页面
    data = {
        "totalPage":totalPage,
        "currentPage":currentPage,
        "news_list":news_list
    }
    return render_template("news/user_collection.html",data=data)



# 修改密码接口
# 获取设置.修改密码
@profile_blue.route('/pass_info',methods=["GET","POST"])
@user_login_data
def pass_info():

    # 判断请求方式.如果get请求
    if request.method == "GET":
        return render_template("news/user_pass_info.html")

    # 如果是post请求,获取参数
    old_password = request.json.get("old_password")
    new_password = request.json.get("new_password")

    # 效验参数,为空效验
    if not all([old_password,new_password]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")

    # 判断老密码是否正确
    if not g.user.check_password(old_password):
        return jsonify(errno=RET.DATAERR,errmsg="老密码错误")

    # 设置新密码
    g.user.password = new_password

    # 返回响应
    return jsonify(errno=RET.OK,errmsg="修改成功")


# 用户上传接口
# 获取设置.用户头像上传
@profile_blue.route('/pic_info', methods=['GET','POST'])
@user_login_data
def pic_info():

    #判断请求方式.
    if request.method == "GET":
        return render_template("news/user_pic_info.html",user_info=g.user.to_dict())

    #3如果是post请求
    #4获取参数
    avatar = request.files.get("avatar")
    #5效验参数
    if not avatar:
        return jsonify(errno=RET.PARAMERR,errmsg="图片不能为空")
    #6上传图像,判断图片是否上传成功
    try:
        image_name = image_storage(avatar.read())
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg="七牛云异常")
    if not image_name:
        return jsonify(errno=RET.NODATA,errmsg="图片上传失败")

    #7将图片设置到用户对象
    g.user.avatar_url = image_name
    #8返回响应
    data = {
        "avatar_url":constants.QINIU_DOMIN_PREFIX + image_name
    }
    return jsonify(errno=RET.OK,errmsg="上传成功",data=data)

# 基本资料
@profile_blue.route('/base_info',methods=['GET','POST'])
@user_login_data
def base_info():

    # 判断请求方式
    if request.method == "GET":
        # 携带数据渲染页面
        return render_template("news/user_base_info.html",user_info=g.user.to_dict())
    # post请求
    # 获取参数
    nick_name = request.json.get("nick_name")
    signature = request.json.get("signature")
    gender = request.json.get("gender")
    #校验参数.为空效验
    if not all([nick_name,signature,gender]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")

    if not gender in ["MAN","WOMAN"]:
        return jsonify(errno=RET.DATAERR,errmsg="性别异常")
    # 修改用户的数据
    g.user.signature = signature
    g.user.nick_name = nick_name
    g.user.gender = gender
    # 返回响应
    return jsonify(errno=RET.OK,errmsg="修改成功")

@profile_blue.route('/user_index')
@user_login_data
def user_index():

    # 1判断用户是否登陆
    if not g.user:
        return redirect("/")

    data = {
        "user_info":g.user.to_dict()
    }
    return render_template("news/user.html",data=data)